import 'package:boss/ui/page/commodity/classificationManagement/page/classificationDetail/provider/classification_detail_provider.dart';
import 'package:boss/ui/widget/form/base_text_form_field.dart';
import 'package:boss/ui/widget/picker/imagePicker/image_picker.dart';
import 'package:boss/ui/widget/title_row_button.dart';
import 'package:boss/ui/widget/form/title_switch.dart';
import 'package:hi_common/hi_common.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';

import 'classification_detail_footer.dart';
import 'classification_detail_material_browser.dart';

class ClassificationDetailBrowser extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: <Widget>[
        Expanded(
          child: SingleChildScrollView(
              padding: EdgeInsets.symmetric(
                horizontal: 30.w,
              ),
              child: Consumer<ClassificationDetailProvider>(
                builder: (context, classificationDetailProvider, child) {
                  return Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: <Widget>[
                      BaseTextFormField(
                        initialValue: classificationDetailProvider.menu.menuName ?? '',
                        prefixText: '分类名称',
                        isRequired: true,
                        hintText: '例如：湘菜、川菜',
                        onChanged: (value) {
                          var classificationDetailProvider = Provider.of<ClassificationDetailProvider>(
                            context,
                            listen: false,
                          );
                          classificationDetailProvider.menu.menuName = value;
                        },
                      ),
                      TitleSwitch(
                        title: '设置为套餐分类',
                        value: classificationDetailProvider.menu.isPackages == 1,
                        onChanged: classificationDetailProvider.toggleIsPackage,
                      ),
                      TitleRowButton(
                        title: '分类图片',
                        hintText: classificationDetailProvider.menu.images == null ? '请上传' : '已上传',
                        subTitle: classificationDetailProvider.menu.images == null ? '所传图片仅在老板端展示,不会在收银机的点菜单中展示' : null,
                        onTap: () async {
                          var image = await ImagePicker.pickImage(context);
                          if (image != null) {
                            var imagePath = await UploadUtil.uploadImage(image);
                            if (imagePath != null) {
                              classificationDetailProvider.setImage(imagePath);
                            }
                          }
                        },
                      ),
                      if (classificationDetailProvider.menu.isPackages == 0) ClassificationDetailMaterialBrowser(),
                    ],
                  );
                },
              )),
        ),
        ClassificationFooter(),
      ],
    );
  }
}
